#pragma once
#include <cassert>
#include <DxErr.h>
#include <Windows.h>
#include <d3dx9.h>
#include "../basiclib/String.h"

#pragma comment( lib, "DxErr.lib" )

#ifndef SAFE_RELEASE
#define SAFE_RELEASE( X )	\
{							\
	if ( X ) {				\
		X->Release();		\
		X = NULL;			\
	}						\
}
#endif

#ifndef SAFE_DELETE
#define SAFE_DELETE( X )	\
{							\
	if ( X ) {				\
		delete X;			\
		X = NULL;			\
	}						\
}							
#endif

#if defined(DEBUG) | defined(_DEBUG)
#ifndef HR
#define HR(x)														\
	   {															\
			HRESULT hr = x;											\
			if( FAILED( hr ) ) {									\
			String func( #x );										\
			DXTraceW( __FILE__, __LINE__, hr, func, TRUE );			\
			}														\
	   }																		
#endif

#else
#ifndef HR
#define HR(x) x;
#endif
#endif

const UINT screen_width  = 640;
const UINT screen_height = 480;

const D3DXVECTOR2 x_normal = D3DXVECTOR2( 1, 0 );
const D3DXVECTOR2 y_normal = D3DXVECTOR2( 0, 1 );

inline D3DXVECTOR3 ObjToScreen( D3DXVECTOR3 pos )
{
	return D3DXVECTOR3( pos.x - 0.5f * screen_width, 0.5f * screen_height - pos.y, pos.z );
}

inline D3DXVECTOR2 ObjToScreen( D3DXVECTOR2 pos )
{
	return D3DXVECTOR2( pos.x - 0.5f * screen_width, 0.5f * screen_height - pos.y );
}

inline RECT RenderArea( D3DXVECTOR2 pos, UINT width, UINT height )
{
	RECT rect;
	rect.top	= pos.y - height * 0.5f;
	rect.left	= pos.x - width * 0.5f;
	rect.bottom = pos.y + height * 0.5f;
	rect.right	= pos.x + width * 0.5f;
	return rect;
}

class TimeCounter
{
public:
							TimeCounter( float timeLimit = 0.4f ):mTimeLimit( timeLimit), mElapsedTime( 0.0f ) { }
	bool					Flies( float dt ) {
								mElapsedTime += dt;
								if ( mElapsedTime > mTimeLimit ) {
									mElapsedTime = 0.0f;
									return true;
								}
								return false;
							}
private:
	float					mTimeLimit;
	float					mElapsedTime;
};

